﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes.sim {
    import Engine.Classes.*;
    import flash.events.*;
    import flash.display.*;
    import flash.geom.*;
    import Engine.Helpers.*;
    import flash.utils.*;
    import Classes.*;
    import Classes.util.*;
    import Classes.effects.*;
    import com.greensock.*;
    import Engine.*;
    import com.zynga.skelly.util.*;

    public class ConsumerShockwave {

        public static const ORTHO_FACTOR:Number = 1.414214;
        protected static const NPC_SHOCKWAVE_RADIUS:Number = 1;
        protected static const NPC_SHOCKWAVE_COLOR:uint = 0x69FF00;
        protected static const NPC_SHOCKWAVE_VELOCITY_FACTOR:Number = 10;

        protected var m_origin:MapResource;
        protected var m_center:Vector3;
        protected var m_velocity:Number;
        protected var m_maxRadius:Number;
        protected var m_npcConsumer:Function;
        protected var m_endCallback:Function;
        protected var m_startTime:int;
        protected var m_seenNpcs:Dictionary;
        protected var m_animation:DisplayObject;
        protected var m_shockwaveColor:uint;
        protected var m_tileRadius:Number;
        protected var m_killTimer:Timer;
        protected var m_fadeHalfway:Boolean;

        public function ConsumerShockwave(_arg1:MapResource, _arg2:Number, _arg3:Number, _arg4:Function=null, _arg5:Function=null, _arg6:Boolean=false, _arg7:uint=0xFFAA00){
            this.m_seenNpcs = new Dictionary(true);
            this.m_origin = _arg1;
            this.m_velocity = _arg2;
            this.m_maxRadius = _arg3;
            this.m_npcConsumer = _arg4;
            this.m_endCallback = _arg5;
            this.m_shockwaveColor = _arg7;
            this.m_fadeHalfway = _arg6;
            var _local8:Vector3 = this.m_origin.getPosition();
            var _local9:Vector3 = this.m_origin.getSize();
            this.m_center = new Vector3((_local8.x + (_local9.x / 2)), (_local8.y + (_local9.y / 2)));
        }
        public function start():void{
            this.m_origin.lock();
            Global.stage.addEventListener(Event.ENTER_FRAME, this.onEnterFrame, false, 0, false);
            this.m_startTime = getTimer();
            var _local1:Point = IsoMath.tilePosToPixelPos(this.m_center.x, this.m_center.y);
            var _local2:DisplayObject = (this.m_animation = new EmbeddedArt.businessShockwave());
            var _local3:ColorTransform = _local2.transform.colorTransform;
            _local3.color = this.m_shockwaveColor;
            _local2.transform.colorTransform = _local3;
            var _local4:String = ((this.m_origin is HarvestableResource)) ? "road" : "default";
            var _local5:Sprite = (Global.world.getObjectLayerByName(_local4).getDisplayObject() as Sprite);
            _local5.addChild(this.m_animation);
            this.m_animation.x = _local1.x;
            this.m_animation.y = _local1.y;
            this.updateVisuals();
        }
        public function stop():void{
            Global.stage.removeEventListener(Event.ENTER_FRAME, this.onEnterFrame);
            var remover:* = Curry.curry(function (_arg1:DisplayObject):void{
                if (_arg1.parent != null){
                    _arg1.parent.removeChild(_arg1);
                };
            }, this.m_animation);
            var fadeout:* = Global.gameSettings.getNumber("bizWaveFade", 1);
            TweenLite.to(this.m_animation, fadeout, {
                alpha:0,
                onComplete:remover
            });
            if (this.m_origin){
                Global.world.citySim.poiManager.clearAllNPCs(this.m_origin);
            };
            this.m_origin.unlock();
            this.m_animation = null;
            this.m_seenNpcs = null;
            this.m_origin = null;
            this.m_npcConsumer = null;
            this.m_endCallback = null;
            this.m_seenNpcs = null;
        }
        protected function onEnterFrame(_arg1:Event):void{
            var candidate:* = null;
            var shockWave:* = null;
            var fadeout:* = NaN;
            var event:* = _arg1;
            if (this.m_origin == null){
                this.stop();
                return;
            };
            this.updateVisuals();
            var checker:* = function (_arg1:WorldObject):Boolean{
                if (((!((_arg1 is NPC))) || (((m_origin as IMerchant) == null)))){
                    return (false);
                };
                var _local2:NPC = (_arg1 as NPC);
                return (((((((((!((_local2 == null))) && (_local2.isFreeAgent))) && (!((m_seenNpcs == null))))) && ((m_seenNpcs[_local2] == null)))) && ((_local2.getPosition().subtract(m_center).length() <= m_tileRadius))));
            };
            var freshmeat:* = Global.world.getObjectsByPredicate(checker);
            if ((((freshmeat.length > 0)) && (!((this.m_npcConsumer == null))))){
                for each (candidate in freshmeat) {
                    this.m_seenNpcs[candidate] = true;
                    shockWave = new ConsumerShockwave(candidate, (this.m_velocity / NPC_SHOCKWAVE_VELOCITY_FACTOR), NPC_SHOCKWAVE_RADIUS, null, null, true);
                    candidate.addAnimatedEffect(EffectType.SHORT_GLOW);
                    TimerUtil.callLater(function ():void{
                        candidate.addAnimatedEffect(EffectType.COIN);
                    }, 800);
                    shockWave.start();
                };
                this.m_npcConsumer(freshmeat);
            };
            if (((this.m_fadeHalfway) && ((this.m_tileRadius >= (this.m_maxRadius / 2))))){
                fadeout = 1.5;
                TweenLite.to(this.m_animation, fadeout, {alpha:0});
                this.m_fadeHalfway = false;
            };
            if (this.m_tileRadius >= this.m_maxRadius){
                if (this.m_endCallback != null){
                    this.m_endCallback();
                } else {
                    this.stop();
                };
            };
        }
        public function setColor(_arg1:uint):void{
            this.m_shockwaveColor = _arg1;
        }
        protected function updateVisuals():void{
            var _local1:Number = ((Constants.TILE_WIDTH * ORTHO_FACTOR) / 200);
            var _local2:Item = this.m_origin.getItem();
            if (_local2 == null){
                this.stop();
                return;
            };
            var _local3:Number = Math.min((_local2.sizeX / 2), (_local2.sizeY / 2));
            var _local4:Number = ((getTimer() - this.m_startTime) / 1000);
            this.m_tileRadius = (_local3 + (_local4 * this.m_velocity));
            this.m_animation.scaleX = (this.m_animation.scaleY = (_local1 * this.m_tileRadius));
            var _local5:Vector3 = this.m_origin.getPosition();
            var _local6:Vector3 = this.m_origin.getSize();
            this.m_center = new Vector3((_local5.x + (_local6.x / 2)), (_local5.y + (_local6.y / 2)));
            var _local7:Point = IsoMath.tilePosToPixelPos(this.m_center.x, this.m_center.y);
            this.m_animation.x = _local7.x;
            this.m_animation.y = _local7.y;
        }

    }
}//package Classes.sim 
